package cn.oslog.mybatis.generator;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

import java.util.List;

/**
 * 分页插件
 * Created by hiswing on 15/4/6.
 */
public class PagingPlugin extends PluginAdapter {

    public boolean validate(List<String> list) {
        return true;
    }

    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addImportedType("com.github.pagehelper.PageHelper");
        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setName("page");
        method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "pageNum"));
        method.addParameter(new Parameter(FullyQualifiedJavaType.getIntInstance(), "pageSize"));
        method.addBodyLine("if (pageSize == 0) pageSize = 10;");
        method.addBodyLine("PageHelper.startPage(pageNum, pageSize);");
        topLevelClass.addMethod(method);
        return true;
    }
}
